package com.parsifal.bluetoothdemo.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.parsifal.bluetoothdemo.R;

/**
 * Created by yangming on 17-3-31.
 */
public class ChoiceDialog extends Dialog {

    public static final int ID = 2;

    private Button button1;//确定按钮
    private Button button2;//取消按钮
    private TextView titleTv;//消息标题文本
    private String titleStr;//从外界设置的title文本
    private String messageStr;//从外界设置的消息文本
    //确定文本和取消文本的显示内容
    private String str1, str2;

    private View.OnClickListener button1ClickListener;//确定按钮被点击了的监听器
    private View.OnClickListener button2ClickListener;//取消按钮被点击了的监听器

    public ChoiceDialog(Context context) {
        super(context, R.style.ChoiceDialog);
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onButton1ClickListener
     */
    public void setButton1OnclickListener(String str, View.OnClickListener onButton1ClickListener) {
        if (str != null) {
            str1 = str;
        }
        this.button1ClickListener = onButton1ClickListener;
    }

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onButton2ClickListener
     */
    public void setButton2OnclickListener(String str, View.OnClickListener onButton2ClickListener) {
        if (str != null) {
            str2 = str;
        }
        this.button2ClickListener = onButton2ClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choice);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (button1ClickListener != null) {
                    button1ClickListener.onClick(v);
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (button2ClickListener != null) {
                    button2ClickListener.onClick(v);
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        //如果设置按钮的文字
        if (str1 != null) {
            button1.setText(str1);
        }
        if (str2 != null) {
            button2.setText(str2);
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        button1 = (Button) findViewById(R.id.btn_connect);
        button2 = (Button) findViewById(R.id.btn_removebond);
        titleTv = (TextView) findViewById(R.id.title);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        messageStr = message;
    }
}